﻿@model Prerequisites.Models.FormModel
@{
    ViewBag.Title = "Formular";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section head{
    <script src="@Url.Content("~/Scripts/utils/date.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/utils/combobox.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#userform").live("submit", function (event) {
                event.preventDefault();
                var form = $(this);
                $("#progressImage").show();
                $.ajax({
                    url: form.attr('action'),
                    type: "POST",
                    data: form.serialize(),
                    success: function (data) {
                        $("#formContainer").html(data);
                        $.validator.unobtrusive.parse("form");
                    },
                    error: function (jqXhr, textStatus, errorThrown) {
                        alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
                    },
                    complete: function () {
                        $("#progressImage").hide();
                    }
                });
            });

            $('#AddDate').datepicker({ dateFormat: 'dd-mm-yy', buttonImageOnly: true, showOn: 'both', buttonImage: '@Url.Content("~/Content/Images/calendar.gif")', onSelect:
                                function () {
                                    $('#AddDate').focus().trigger({ type: 'keypress', charCode: 13 });
                                }
            }).width(175);
            $('.ui-datepicker-trigger').css('float', 'left');

            $('#Option').combobox();

            $("#dialogForm").dialog({
                autoOpen: false,
                height: 300,
                width: 620,
                modal: true,
                buttons: {
                    "Save": function () {
                        var bValid = true;

                        //pot adauga logica de validare, de exemplu:
                        //allFields.removeClass("ui-state-error");
                        //bValid = bValid && checkLength(name, "username", 3, 16);

                        if (bValid) {
                            //salveaza etc
                            $(this).dialog("close");
                        }
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                },
                close: function () {
                    allFields.val("").removeClass("ui-state-error");
                }
            });

            $("#buttonDialog")
			.button()
			.click(function () {
			    $("#dialogForm").dialog("open");
			});

        });

        //regulile custom de unobtrusive trebuie apelate inainte de incarcare documentului
        // validator parseaza formularul pentru reguli
        //, (function() poate sa dispara, e bun doar pentru grupare
        (function () {
            //adaug unobtrusive pentru atributul MustBe
            $.validator.addMethod('mustbe', function (value, element, params) {
                var testValue = params['propertyvalue'];
                var condition = params['condition'];
                if (condition == '0' && value != testValue)
                    return true;
                else if (condition == '1' && value == testValue)
                    return true;
                else
                    return false;
            });

            var setValidationValues = function (options, ruleName, value) {
                options.rules[ruleName] = value;
                if (options.message) {
                    options.messages[ruleName] = options.message;
                }
            };

            $.validator.unobtrusive.adapters.add('mustbe', ['propertyvalue', 'condition'], function (options) {
                var value = {
                    propertyvalue: options.params.propertyvalue,
                    condition: options.params.condition
                };
                setValidationValues(options, "mustbe", value);
            });

        })();

    </script>
}

<div id='dialogForm'>
    @Html.Partial("Form", Model)
</div>
<br />
<button id='buttonDialog'>Arata dialog</button>